//
//  SYDNavigationViewController.swift
//  Swift4.0_Learn
//
//  Created by lantaiyuan on 2017/10/24.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit

class SYDNavigationViewController: UINavigationController {

    
    var PopGestureRecognizerEnable : Bool
    var pan : UIPanGestureRecognizer
    
    override convenience init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        self.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        setBarButtonItem()
        setNavigationBar()
        
        if self.PopGestureRecognizerEnable {
            pan.delegate = self
        } else {
            pan.delegate = nil
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.isTranslucent = false
        self.navigationBar.isHidden = true
        
        self.interactivePopGestureRecognizer?.isEnabled = false
        // Do any additional setup after loading the view.
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setBarButtonItem() {
        // 设置整个项目所在item的主题样式
        let item = UIBarButtonItem.appearance()
        // 设置普通状态
        let textAttrs = NSMutableDictionary.init()
        textAttrs[NSForegroundColorAttributeName] = UIColor.white;
        textAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 15);
        item.setTitleTextAttributes(textAttrs as? [String : Any], for: .normal)
        
        // 设置不可用状态
        let disableAttrs = NSMutableDictionary.init()
        
        disableAttrs[NSForegroundColorAttributeName] = UIColor.init(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.4)
        
        disableAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 13)
        item.setTitleTextAttributes(disableAttrs as? [String : Any], for: .disabled)
        
        // 返回按钮
        let iponeM = UIDevice.current.systemVersion
        let sepArr = iponeM.components(separatedBy: ".")
        
        if stringToInt(str: sepArr[0]) < 9 {
            
            var offSet:UIOffset?
            offSet?.horizontal = -500
            let backButtonItem: UIBarButtonItem = UIBarButtonItem.appearance()
            backButtonItem.setBackButtonTitlePositionAdjustment(offSet!, for: .default)
            
        } else {
            
            let backButtonItem: UIBarButtonItem = UIBarButtonItem.appearance()
            var offSet:UIOffset?
            offSet?.horizontal = -500
            backButtonItem.setBackButtonTitlePositionAdjustment(offSet!, for: .default)        }
        
    }
    func stringToInt(str:String)->(Int){
        
        let string = str
        var int: Int?
        if let doubleValue = Int(string) {
            int = Int(doubleValue)
        }
        if int == nil
        {
            return 0
        }
        return int!
    }
    private func setNavigationBar() {
        
        let navigationBar = UINavigationBar.appearance()
        // 设置导航栏颜色
        navigationBar.barTintColor = GREEN_COLOR;
        
        // 设置导航栏标题颜色
        let attrs = NSMutableDictionary.init()
        attrs[NSForegroundColorAttributeName] = NAV_TITLE_COLOR;
        attrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 20)
        navigationBar.titleTextAttributes = attrs as? [String : Any];
        navigationBar.tintColor = WHITE_COLOR;
        
        //去除导航栏底线
        navigationBar.setBackgroundImage(UIImage.init(), for: .default)
        navigationBar.shadowImage = UIImage.init()
    }
    // 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
    // 作用：拦截手势触发
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
        // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
        if self.childViewControllers.count == 1 {
            // 表示用户在根控制器界面，就不需要触发滑动手势，
            return false
        }
        return true
    }
    /**
     *  重写这个方法目的：能够拦截所有push进来的控制器
     *
     *  @param viewController 即将push进来的控制器
     */
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {// 这是push进来的控制器不是第一个控制器
            /*自动隐藏TabBar*/
            viewController.hidesBottomBarWhenPushed = true
            
            // 设置导航栏内容
            // 设置左边的返回按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.itemWithTargetActionImageHeightImage(target: self, action: #selector(back), image: "nav_button_return" , hightImage: "nav_button_return")
            // 设置右边的更多按钮
            viewController.navigationItem.rightBarButtonItem = UIBarButtonItem.itemWithTargetActionImageHeightImage(target: self, action: #selector(more), image: "navigationbar_more", hightImage: "navigationbar_more_highlighted")
        }
        super.pushViewController(viewController, animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func back() {
        // 因为self本来就是一个控制器，self.navigationController这时是nil
        self.popViewController(animated: true)
    }
    func more() {
        self .popToRootViewController(animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
